package com.xii.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xii.commonutils.R;
import com.xii.eduservice.entity.EduTeacher;
import com.xii.eduservice.entity.vo.TeacherQuery;
import com.xii.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author xhjava
 * @since 2022-01-16
 */
@Api(description = "讲师管理") //在swagger上的描述
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin    //跨域
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    //查询
    @GetMapping("findAll")
    @ApiOperation(value = "所有讲师列表")
    public R list() {
        //mp中的list方法  查询所有
        List<EduTeacher> list = eduTeacherService.list(null);
//        try{
//            int a = 10/0;
//        }catch (XiiException e){
//            throw new XiiException(20001,"自定义异常");
//        }
        return R.ok().data("items", list);
    }


    //逻辑删除  记得逻辑删除插件
    @ApiOperation(value = "根据ID逻辑删除") //在swagger上的描述
    @DeleteMapping("{id}")
    public R removeById(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id) {   //ApiParam 在swagger上的描述

        boolean b = eduTeacherService.removeById(id);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //分页
    @ApiOperation(value = "分页")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,  //当前页
                             @PathVariable long limit) {   //每页记录数
        //创建page对象
        Page<EduTeacher> pageteacher = new Page<>(current, limit);
        //调用方法实现分页
        //调用方法 底层封装 会把所有数据封装到pageteacher中
        eduTeacherService.page(pageteacher, null);
        long total = pageteacher.getTotal(); //总记录数
        List<EduTeacher> records = pageteacher.getRecords(); //每页数据的集合
        return R.ok().data("total", total).data("rows", records);
    }


    //分页带条件查询
    @ApiOperation(value = "分页条件查询")
    @PostMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacherQuery(@PathVariable long current,  //当前页
                                  @PathVariable long limit,//每页记录数
//                                  @RequestBody 将前端传过来的值转化为对象 注意和属性名字要相同
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {   //@RequestBody(required = false) 可以为空 注意使用RequestBody 只适用post提交
        //创建page对象
        Page<EduTeacher> pageteacher = new Page<>(current, limit);
        //增加条件
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        //多条件组合查询
        //mybatis动态sql=
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断你的条件是否为空
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            queryWrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_create",end);
        }

        //调用方法实现分页
        //调用方法 底层封装 会把所有数据封装到pageteacher中
        eduTeacherService.page(pageteacher, queryWrapper);
        long total = pageteacher.getTotal(); //总记录数
        List<EduTeacher> records = pageteacher.getRecords(); //每页数据的集合
        return R.ok().data("total", total).data("rows", records);
    }


    //添加操作
    @ApiOperation(value = "添加老师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = eduTeacherService.save(eduTeacher);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    //根据id查询
    @ApiOperation(value = "根据id查询")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher byId = eduTeacherService.getById(id);
        return R.ok().data("teacher",byId);
    }

    //更新操作
    @ApiOperation(value = "根据id更新")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = eduTeacherService.updateById(eduTeacher);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }



}

